<template>
  <div class="crash">
    <label for="">宕机账号：</label>
    <el-autocomplete
      class="inline-input"
      v-model="sid"
      :fetch-suggestions="querySearch"
      placeholder="请输入学号/手机"
      :trigger-on-focus="false"
      @select="handleSelect"
    >
      <template slot-scope="{ item }">
        <div style="display: flex">
          <div class="name">学号:{{ item.value }} &</div>
          <div class="addr">{{ item.address }}</div>
        </div>
      </template>
    </el-autocomplete>
    <el-button type="primary" style="margin-left: 10px" @click="restore"
      >重新授权</el-button
    >

  </div>
</template>

<script>
export default {
  data() {
    return {
      restaurants: [],
      state1: "",
      state2: "",
      sid: null,
    };
  },
  created() {
    this.getStudentList();
  },
  methods: {
    async getStudentList() {
      const res = await this.$http.get("/management/userList");
      console.log(res);
      const data = [];
      res.data.data.filter((item) => {
        data.push({
          value: item.sid,
          address:
            "姓名:" +
            this.isNull(item.name) +
            " - 班级:" +
            this.isNull(item.grade),
        });
      });
      this.restaurants = data;
      console.log(this.restaurants);
    },
    isNull(val) {
      return val == null ? val : val.length == 0 ? null : val;
    },
    querySearch(queryString, cb) {
      console.log("执行");
      var restaurants = this.restaurants;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },
    loadAll() {
      return [
        // { value: "三全鲜食（北新泾店）", address: "长宁区新渔路144号" },
        // {
        //   value: "Hot honey 首尔炸鸡（仙霞路）",
        //   address: "上海市长宁区淞虹路661号",
        // },
        // {
        //   value: "南拳妈妈龙虾盖浇饭",
        //   address: "普陀区金沙江路1699号鑫乐惠美食广场A13",
        // },
      ];
    },
    handleSelect(item) {
      console.log(item);
      this.sid = item.value;
    },
    // /management/restore
    async restore() {
      if(!(this.sid??this.state1)){
        return this.$message.info("请填写正确的学号")
      }
      console.log(this.sid,this.state1);
      const res = await this.$http({
        url: "/management/restore",
        method: "POST",
        params: {
          sid: this.sid??this.state1,
        },
      });
      console.log(res);
      if(res.data.message!='ok'){
        return this.$message.error(res.data.data)
      }
       this.$message.success("重新授权成功")
    },
  },
  mounted() {
    // this.restaurants = this.loadAll();
  },
};
</script>
<style lang="less" scoped>
.inline-input {
  width: 500px;
}
.crash {
  text-align: left;
}
</style>